package com.cbd.basic.design_pattern.service.impl;

import com.cbd.basic.design_pattern.service.FinManageStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class FinManageStrategyFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private static final Map<String, FinManageStrategy> STRATEGIES = new HashMap<>();

//    static {
//        // 预先注册所有已知的融资策略
//        STRATEGIES.put("bankloan", new com.yy.tms.service.impl.BankLoanStrategy());
//        STRATEGIES.put("tradefin", new com.yy.tms.service.impl.TradeFinStrategy());
//        // 添加新的融资方式只需在这里加一行
//    }

    public static FinManageStrategy getStrategy(String finType) {
        if (finType == null || finType.isEmpty()) {
            throw new IllegalArgumentException("finType should not be empty.");
        }
        FinManageStrategy strategy = STRATEGIES.get(finType.toLowerCase());
        if (strategy == null) {
            throw new RuntimeException("No strategy found for finType: " + finType);
        }
        return strategy;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void initFinManageStrategy() {
        //applicationContext.getBeansOfType()
    }
}
